package com.example.healthcode2.Fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import com.example.healthcode2.Object.yiqingdata;
import com.example.healthcode2.WebActivity;
import com.example.healthcode2.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;

public class twoFragment extends Fragment {
    private View view;
    private View view_include_1, view_include_2, view_include_3;
    private Button button1, button2;

    public twoFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_two, container, false);
        view_include_1 = view.findViewById(R.id.two_layout_1);
        view_include_2 = view.findViewById(R.id.two_layout_2);
        view_include_3 = view.findViewById(R.id.two_layout_3);
        initBtn();
        jsonRequest();
        return view;
    }

    private void initBtn() {
        button1 = view.findViewById(R.id.two_btn_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), WebActivity.class);
                String url = "http://192.168.23.1:8080/webfinal/123/china.html";
                intent.putExtra("url", url);
                intent.putExtra("title", "国内疫情");
                startActivity(intent);
            }
        });
        button2 = view.findViewById(R.id.two_btn_2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), WebActivity.class);
                String url = "http://192.168.23.1:8080/webfinal/123/foreign.html";
                intent.putExtra("url", url);
                intent.putExtra("title", "海外疫情");
                startActivity(intent);
            }
        });

    }

    private void jsonRequest() {
        RequestQueue queue = Volley.newRequestQueue(getContext());
        String url = "http://30r97104b2.qicp.vip/user/message";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {

                        try {
                            JSONArray jsonArray = jsonObject.getJSONArray("yiqingdata");
                            Gson gson = new Gson();
                            String a = jsonArray.toString();
                            Type type = new TypeToken<ArrayList<yiqingdata>>() {
                            }.getType();

                            ArrayList<yiqingdata> list = gson.fromJson(a, type);
                            Message message = new Message();
                            message.obj = list;
                            myHandler.sendMessage(message);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        myHandler.sendEmptyMessage(2);

                    }
                });

        queue.add(request);
    }

    @SuppressLint("HandlerLeak")
    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 2) {
                Toast.makeText(getContext(), "疫情数据获取失败", Toast.LENGTH_SHORT).show();
            }
            ArrayList<yiqingdata> list = (ArrayList<yiqingdata>) msg.obj;
            if (list!=null) {
                showyiqingdata(view_include_1, list.get(0));
                showyiqingdata(view_include_2, list.get(1));
                showyiqingdata(view_include_3, list.get(2));

            }
        }
    };

    private void showyiqingdata(View view, yiqingdata yiqingdata) {
        TextView t1 = view.findViewById(R.id.two_text_a0);
        TextView t2 = view.findViewById(R.id.two_text_a1);
        TextView t3 = view.findViewById(R.id.two_text_a2);
        TextView t4 = view.findViewById(R.id.two_text_a3);
        TextView t5 = view.findViewById(R.id.two_text_a4);
        if (yiqingdata != null) {
            t1.setText(yiqingdata.getPlacename());
            t2.setText(yiqingdata.getNowquezhen());
            t3.setText(yiqingdata.getSumquezhen());
            t4.setText(yiqingdata.getSumdead());
            t5.setText(yiqingdata.getSumcure());
        }
    }


    //生成json数据
    private void initjson() {
        ArrayList<yiqingdata> list = new ArrayList<>();
        yiqingdata a1 = new yiqingdata("宁波疫情", "0", "157", "0", "157");

        yiqingdata a2 = new yiqingdata("国内疫情", "984", "84369", "4643 ", "78742");
        yiqingdata a3 = new yiqingdata("海外疫情", "1958327", "3065252", "892662", "214263");
        Gson gson = new Gson();
        list.add(a1);
        list.add(a2);
        list.add(a3);
        Type type = new TypeToken<ArrayList<yiqingdata>>() {
        }.getType();

        String json = gson.toJson(list, type);
        System.out.println(json);
    }


}
